package com.zhongshan.ga.mobile.logic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.SqlSession;
import org.restlet.representation.Representation;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongshan.ga.commonvar.ArgsConfig;
import com.zhongshan.ga.commonvar.CommendConfig;
import com.zhongshan.ga.commonvar.PHPWebUrl;
import com.zhongshan.ga.logger.GenerateLog;
import com.zhongshan.ga.mybatis.bean.SelectTravedlMateListInfo;
import com.zhongshan.ga.mybatis.bean.SpecialLocalProduct;
import com.zhongshan.ga.mybatis.bean.TravelMateImgBean;
import com.zhongshan.ga.mybatis.dao.ITravelMateInfoDao;
import com.zhongshan.ga.utils.MyBatisUtil;

/**
 * 获取结伴游列表逻辑处理类
 * @author yanqi
 *
 */
public class GetTravelMateListLogic extends ParentLogic {
	/**
	 * 获得结伴游列表逻辑处理方法
	 * @param representation 客户端请求体
	 * @return JsonObject 处理结果
	 */
	public JsonObject getTravelMateList(Representation representation) {
		JsonObject resultJson = new JsonObject();
		Gson gson = new Gson();
		try {
			// 获取请求中的json
			String requestText = representation.getText();
			GenerateLog.outDebug(requestText);
			JsonObject acceptJson = new JsonParser().parse(requestText)
					.getAsJsonObject();
			GenerateLog.outDebug(acceptJson);
			
			/**
			 * 进行基本的逻辑较验
			 */
			resultJson = super.basicCheck(acceptJson, CommendConfig.TRAVEL_MATE_INFO_LIST, false);
			
			if (resultJson.get(ArgsConfig.RESULT_CODE).getAsString()
					.equals(ArgsConfig.RESPONSE_SUCCESS)) {
				// 调用接口 获取数据
				SqlSession session = MyBatisUtil.getSqlSession(true);
				// 获得请求的数据体
				 JsonObject dataJsonObject = acceptJson.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(dataJsonObject);
				
				// 获得请求的记录下标
				int startIndex = dataJsonObject.get(ArgsConfig.START_INDEX).getAsInt();
				int endIndex = dataJsonObject.get(ArgsConfig.RECORD_NUM).getAsInt();
				
				// 设置条件参数
				Map<String, Object> paramMap = new HashedMap();
				paramMap.put("startIndex", startIndex);
				paramMap.put("endIndex", endIndex);
				
				/**
				 * 获得数据
				 */
				List<SelectTravedlMateListInfo> travelMateList = session.selectList("com.mybatis.mapping.travelMateMapper.selectTravelMateList", paramMap);
				if (travelMateList != null) {
					// 图片连接拼接
					for (SelectTravedlMateListInfo item : travelMateList) {
						ArrayList<TravelMateImgBean> imgBeans = item.getTravelMateImgBeans();
						if (imgBeans != null) {
							for (TravelMateImgBean imgBean : imgBeans) {
								imgBean.setImageName(PHPWebUrl.TRAVEL_MATE_IMAGES_URL + imgBean.getImageName());
							}
							item.setTravelMateImgBeans(imgBeans);
						}
					}
				}
				
				/**
				 * 获得每次查询的记录总数
				 */
				String reObject = session.selectOne("com.mybatis.mapping.travelMateMapper.selectTravelMateListCount", paramMap);
				if(reObject == null) {
					reObject = "0";
				}
				GenerateLog.outDebug(reObject.toString());
				GenerateLog.outDebug("****************************结果数据为："+ travelMateList.toString() +"**********************");
				JsonParser jsonParser = new JsonParser();
				JsonArray jsonArray = jsonParser.parse(gson.toJson(travelMateList)).getAsJsonArray();
				GenerateLog.outDebug("****************************结果数据为："+ jsonArray +"**********************");
				
				JsonObject tempJsonObject = acceptJson.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(tempJsonObject);
				
				ITravelMateInfoDao travelMateInfoDao = session.getMapper(ITravelMateInfoDao.class);
				/**
				 * 的服务端返回的结果数据中要加入 评论数，和一个加入的人数
				 */
				for (int i = 0; i < jsonArray.size(); i++) {
					/**
					 * 获结伴游编码
					 */
					JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
					GenerateLog.outDebug("原来的结果数据"+"--"+ i +"--"+ jsonObject);
					String travelMateCode = jsonObject.get(ArgsConfig.TRAVEL_MATE_CODE).getAsString();
					GenerateLog.outDebug("取得的结伴游编码"+"--"+ i +"--"+ travelMateCode );
					
					/**
					 * 获得对应于结伴游编码的评论数和加入人数
					 */
					int commentNum = travelMateInfoDao.selectTravelCommentCount(travelMateCode);
					int joinPeopleNum = travelMateInfoDao.selectTravelMateLsitCount(travelMateCode);
					GenerateLog.outDebug("加入人数"+"--"+ i +"--"+ joinPeopleNum );
					GenerateLog.outDebug("评论数"+"--"+ i +"--"+ commentNum );
					/**
					 * 加入json对象中
					 */
					jsonObject.addProperty(ArgsConfig.COMMENT_NUM, commentNum);
					jsonObject.addProperty(ArgsConfig.JOIN_PEOPLE_NUM, joinPeopleNum);
					GenerateLog.outDebug("后来的结果数据"+"--"+ i +"--"+ jsonObject);
					
					jsonArray.set(i, jsonObject);
					
					GenerateLog.outDebug("后来的数据数据"+"--"+ i +"--"+ jsonArray);
				}
				
				GenerateLog.outDebug("最后的数据数据"+ jsonArray);
				
				tempJsonObject.add(ArgsConfig.RESULT_DATA, jsonArray);
				GenerateLog.outDebug(tempJsonObject+"11111111111111111");
				resultJson.add(ArgsConfig.REQUEST_DATA, acceptJson.get(ArgsConfig.REQUEST_DATA).getAsJsonObject());
				resultJson.addProperty(ArgsConfig.TOTAL_RECORD_NUM_EVERY_TIME, Integer.valueOf(reObject));
				GenerateLog.outDebug("***************************返回的数据：" + resultJson + "**********************************");
				
			}
			return resultJson;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}
